package com.ebao.action.user;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.ebao.action.base.BaseAction;
import com.ebao.constants.UserType;
import com.ebao.constants.WebConstants;
import com.ebao.po.doctor.TDoctor;
import com.ebao.po.doctor.THospital;
import com.ebao.po.user.TUser;
import com.ebao.po.user.TUserInfo;
import com.ebao.po.user.TUserRegister;
import com.ebao.service.doctor.IDoctorService;
import com.ebao.service.user.IUserRegisterService;
import com.ebao.service.user.IUserService;
import com.ebao.util.encrypt.MD5;

/**
 * @author snow
 * @date 2012-7-17
 */
public class UserRegisterAction extends BaseAction {

	private static final long serialVersionUID = -6799977600580837827L;

	private String username;	// 用户名
	private String email;		// 邮箱
	private String info;		// 简介
	
	private Long id;
	private String encrypt;			// 邮箱中经过MD5加密后的医生邮箱
	
	@Resource
	private IDoctorService doctorService;
	@Resource
	private IUserService userService;
	@Resource
	private IUserRegisterService userRegisterService;
	
	private String jsonInfo;
	
	/**
	 * 用户/医生 申请注册时检查邮箱是否已存在
	 */
	@Action(value = "check-email", results = {@Result(name = SUCCESS, 
			type = "json", params = {"root","jsonInfo"})})
	public String checkEmail(){
		String jpql = "from TUserRegister u where u.email=?";
		List<TUserRegister> userRegisters = userRegisterService.getListByJpql(jpql, email.trim());
		if(CollectionUtils.isNotEmpty(userRegisters)){
			jsonInfo = "error";		// 邮箱已存在
		} else {
			jsonInfo = "success";
		}
		
		return SUCCESS;
	}
	
	/**
	 * 用户/医生 申请注册时检查是否有同名
	 */
	@Action(value = "check-name", results = {@Result(name = SUCCESS, 
			type = "json", params = {"root","jsonInfo"})})
	public String checkName(){
		String jpql = "from TUserRegister u where u.userName=?";
		List<TUserRegister> userRegisters = userRegisterService.getListByJpql(jpql, username.trim());
		if(CollectionUtils.isNotEmpty(userRegisters)){
			jsonInfo = "error";		// 已存在该用户名
		} else {
			jsonInfo = "success";
		}
		
		return SUCCESS;
	}
	
	/**
	 * 用户申请注册
	 */
	@Action(value = "user-register", results = {@Result(name = SUCCESS, location = "/index.jsp")})
	public String userRegister(){
		TUserRegister UserRegister = new TUserRegister();
		UserRegister.setUserName(username.trim());
		UserRegister.setEmail(email.trim());
		UserRegister.setIntroduction(info.trim());
		UserRegister.setRegisterDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		UserRegister.setUserType(UserType.COMMON_USER);
		UserRegister.setStatus(0);
		userRegisterService.save(UserRegister);
		return SUCCESS;
	}

	/**
	 * 医生申请注册
	 */
	@Action(value = "doctor-register", results = {@Result(name = SUCCESS, location = "/index.jsp")})
	public String doctorRegister(){
		TUserRegister doctorRegister = new TUserRegister();
		doctorRegister.setUserName(username.trim());
		doctorRegister.setEmail(email.trim());
		doctorRegister.setIntroduction(info.trim());
		doctorRegister.setRegisterDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		doctorRegister.setUserType(UserType.DOCTOR);
		doctorRegister.setStatus(0);
		userRegisterService.save(doctorRegister);
		return SUCCESS;
	}

	/**
	 * 用户在邮箱中点击链接，激活账户
	 * 跳转到个人资料设置密码页
	 */
	@Action(value = "user-confirmRegister", results = {@Result(name = SUCCESS, 
			location = "userset.jsp?fpage=passSet.jsp")})
	public String userConfirmRegister(){
		TUserRegister UserRegister = userRegisterService.getById(id);
		if(encrypt.equals(MD5.MD5Encode(UserRegister.getEmail()))){// 激活账户
			String jpql = "from TUser where email=?";
			TUser user = userService.getByJpql(jpql, UserRegister.getEmail());
			if(user == null){
				user = new TUser();
				user.setEmail(UserRegister.getEmail());
				user.setUserName(UserRegister.getUserName());
				TUserInfo userInfo= new TUserInfo();
				userInfo.setRegisterDate(UserRegister.getRegisterDate());
				user.setUserInfo(userInfo);
				userService.save(user);
			}
			
			session.put(WebConstants.LOGIN_USER, user);
		}
		
		return SUCCESS;
	}
	
	/**
	 * 医生在邮箱中点击链接，激活账户
	 * 跳转到个人资料设置密码页
	 */
	@Action(value = "doctor-confirmRegister", results = {@Result(name = SUCCESS, 
			location = "../doctor/doctorset.jsp?fpage=passSet.jsp")})
	public String doctorConfirmRegister(){
		TUserRegister UserRegister = userRegisterService.getById(id);
		if(encrypt.equals(MD5.MD5Encode(UserRegister.getEmail()))){// 激活账户
			String jpql = "from TDoctor where email=?";
			TDoctor doctor = doctorService.getByJpql(jpql, UserRegister.getEmail());
			if(doctor == null) {
				doctor = new TDoctor();
				doctor.setEmail(UserRegister.getEmail());
				doctor.setUserName(UserRegister.getUserName());
				TUserInfo doctorInfo= new TUserInfo();
				doctorInfo.setRegisterDate(UserRegister.getRegisterDate());
				doctor.setUserInfo(doctorInfo);
				doctor.setHospital(new THospital());
				doctorService.save(doctor);
			}
			session.put(WebConstants.LOGIN_DOCTOR, doctor);
		}
		
		return SUCCESS;
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getEncrypt() {
		return encrypt;
	}

	public void setEncrypt(String encrypt) {
		this.encrypt = encrypt;
	}

	public String getJsonInfo() {
		return jsonInfo;
	}

	public void setJsonInfo(String jsonInfo) {
		this.jsonInfo = jsonInfo;
	}

}
